import Vue from 'vue'
import App from './App.vue'
import store from './store'
import VueRouter from 'vue-router'
import routes from './router/router'
import 'lib-flexible/flexible.js'
import md5 from 'js-md5';
import 'swiper/dist/css/swiper.min.css'
import 'swiper/dist/js/swiper.min'
Vue.config.productionTip = false

import Vant from 'vant';
import 'vant/lib/index.css';
Vue.use(Vant);

Vue.prototype.$md5 = md5;

Vue.use(VueRouter);
//路由守卫
const router = new VueRouter({
  // mode: 'history',
  routes
});
/* 路由异常错误处理，尝试解析一个异步组件时发生错误，重新渲染目标页面 */
router.onError((error) => {
  const pattern = /Loading chunk (\d)+ failed/g
  const isChunkLoadFailed = error.message.match(pattern)
  // const targetPath = router.history.pending.fullPath
  if (isChunkLoadFailed) {
    //  router.replace(targetPath)
    location.reload()
  }
})

router.beforeEach((route, redirect, next) => {
  if (route.path === '/login') {
    localStorage.removeItem('Authorization')
  }
  let user = localStorage.getItem('Authorization');
  console.log(route.meta.requireAuth==true)
  if (route.meta.requireAuth) {
    if (!user && route.path !== '/login') {
    next({ path: '/login' })
  } else {
    if (route.name != '') {
      next()
    } else {
      next({ path: '/home' })
    }
  }
  }else{
    next()
  }
  


})

new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})

export default router;
